/*
 * ListScsi.m
 *    - This, and the main routine it calls (do_inquiryall), were
 *	written by Garance Alistair Drosehn/Mar 1994.  Note that
 *	do_inquiryall may not be safe to call at all times.  For the
 *	most part this works fine for me, but occasionally it will
 *	totally lock up my machine for a minute or so.  Thus I do
 *	not recommend it's use (until someone can fix do_inquiryall).
 */

#import <ctype.h>
#import "ListScsi.h"

@implementation ListScsi

- init
{
    int scsiIndex, diskIndex;
    
    do_inquiryall( INQ_ALL_MAX_SCSI_CNT, &inquireAllResult, &inquire_Ereply);

    diskIndex = 0;
    for(scsiIndex = 0; scsiIndex < inquireAllResult.maxScsiCount;
    		       scsiIndex++) {
	if ( inquireAllResult.scsiArray[scsiIndex].deviceNumber >= 0 ) {
	    /* there is a device with this SCSI ID index, remember
	     * the SCSI id and increment the disk index counter
	     */
	    mapDiskToScsiId[diskIndex] = scsiIndex;
	    diskIndex++;
	    }
	}		/* end for(scsiIndex =... */

    /* fill up the rest of the DiskToScsiId array */
    for( ; diskIndex < INQ_ALL_MAX_SCSI_CNT; diskIndex++) {
	mapDiskToScsiId[diskIndex] = -1;
	}
    
    return self;
}

- (int) _scsiIndex:(int) devIndex
{
    if ((devIndex < 0) || (devIndex >= INQ_ALL_MAX_SCSI_CNT)) {
	return -1;
	}
    return mapDiskToScsiId[devIndex];
}

- (int) getTypeOfDev:(int) devNumber
{
    struct inquiry_reply *mInq;
    int scsiIndex = [self _scsiIndex:devNumber];

    if (scsiIndex < 0) return DEVTYPE_NOTPRESENT;

    mInq = &(inquireAllResult.scsiArray[scsiIndex].inqResult);
    return mInq->ir_devicetype;
}

- (int) getTypeOfDev:(int) devNumber retCharDescription:(char *)devStr
{
    struct inquiry_reply *mInq;
    char   tempStr[20];
    int    charIndex;
    int    scsiIndex = [self _scsiIndex:devNumber];

    if (scsiIndex < 0) {
	sprintf (devStr, "sd%d - no device attached", devNumber);
	return DEVTYPE_NOTPRESENT;
	}

    mInq = &(inquireAllResult.scsiArray[scsiIndex].inqResult);
    sprintf (devStr, "sd%d [%d]: ", devNumber, scsiIndex);

    /* tack on the vendor string, trimmed and prettified */
    *tempStr = '\0';
    strncat(tempStr, mInq->ir_vendorid, sizeof(mInq->ir_vendorid));
    tempStr[sizeof(mInq->ir_vendorid)] = '\0';
    for (charIndex = sizeof(mInq->ir_vendorid) - 1;
    		charIndex > 0; charIndex--) {
	if (tempStr[charIndex] == ' ') tempStr[charIndex] = '\0';
	else break;
	}
    for ( ; charIndex > 0; charIndex--) {
	if ( isupper(tempStr[charIndex]) )
	    tempStr[charIndex] = tolower(tempStr[charIndex]);
	}
    strcat(devStr, tempStr);
    
    /* tack on the product ID string, trimmed */
    strcat(devStr, " ");
    *tempStr = '\0';
    strncat(tempStr, mInq->ir_productid, sizeof(mInq->ir_productid));
    tempStr[sizeof(mInq->ir_productid)] = '\0';
    for (charIndex = sizeof(mInq->ir_productid) - 1;
    		charIndex > 0; charIndex--) {
	if (tempStr[charIndex] == ' ') tempStr[charIndex] = '\0';
	else break;
	}
    strcat(devStr, tempStr);

    /* tack on the revision info, trimmed, if it's there */
    if ( (mInq->ir_revision[0] != ' ') && (mInq->ir_revision[0] != '\0')) {
	strcat(devStr, " (rev. ");
	*tempStr = '\0';
	strncat(tempStr, mInq->ir_revision, sizeof(mInq->ir_revision));
	tempStr[sizeof(mInq->ir_revision)] = '\0';
	for (charIndex = sizeof(mInq->ir_revision) - 1;
		    charIndex > 0; charIndex--) {
	    if (tempStr[charIndex] == ' ') tempStr[charIndex] = '\0';
	    else break;
	    }
	strcat(devStr, tempStr);
	strcat(devStr, ")");
	}

    return mInq->ir_devicetype;
}


@end
